/**   
 * Copyright © 2017 yswwpp. All rights reserved.
 * 
 * @Title: LineResolver.java 
 * @Prject: cross-common
 * @Package: com.yswwpp.cross.http.util 
 * @Description: TODO
 * @author: ysw   
 * @date: 2017年9月18日 下午10:17:28 
 * @version: V1.0   
 */
package com.yswwpp.cross.http.util;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

import com.yswwpp.cross.http.Protocol;

import io.netty.buffer.ByteBuf;

/**
 * @ClassName: LineResolver
 * @Description: TODO
 * @author: ysw
 * @date: 2017年9月18日 下午10:17:28
 */
public class LineResolver implements Protocol
{
	public static String readLine(ByteBuf in)
	{

		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while (in.isReadable())
		{
			byte temp = in.readByte();
			if (temp == HTTP_PROTOCOL_C_R)
			{

				byte next = in.readByte();

				if (temp == HTTP_PROTOCOL_N_F)
				{
					break;
				} else
				{
					bos.write(temp);
					bos.write(next);
				}
			} else
			{
				bos.write(temp);
			}
		}
		
		try
		{
			return new String(bos.toByteArray(),"UTF-8");
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
			return null;
		}
	}
}
